// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.
#include "Import/BDFImport/BulkData/Dynamic/DTIParser.h"

#include "gtest/gtest.h"

#include "DataStructure/Common/Constant.h"
#include "DataStructure/Input/Dynamic/SPECSELData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Dynamic/ISPECSELService.h"
#include "DBManager/IService/Input/Dynamic/ISPSELService.h"

#include "Import/BDFImport/BDFParserBase.h"

using SG::DataStructure::Common::TOLERANCE;
using namespace SG::DataStructure::FEM;
using namespace SG::DBManager;
using namespace SG::Import;

extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;

class DTIParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
    }
};
TEST_F (DTIParserTest, ParseEntry_OnlyRECNO)
{
    // DTI, SPECSEL, 0
    std::vector<std::string> Entry = { "DTI,SPECSEL,0" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);
    auto status = SG::Import::BDF::BULKDATA::ParseDTI (DB_SERVICEFACTORY, dividedEntryInfo);
    ASSERT_EQ (status, SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::SPECSELData data;
    ASSERT_EQ (DB_SERVICEFACTORY->get<ISPECSELService> ()->FindById (0, data), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (data.m_id, 0);
    ASSERT_STREQ (data.m_TYPE, "");
    ASSERT_EQ (data.m_TABLESList.size (), 0);
}

TEST_F (DTIParserTest, ParseEntry_MultiLineAndEmptyFields)
{
    // 模拟多行拼接和部分字段为空的情况
    // 形式: DTI, SPECSEL, 1,  , A, 2, 0.0, 3, 0.02,
    //       , 4, 0.04, ENDREC
    std::vector<std::string> Entry = { "DTI, SPECSEL, 1,  , A, 2, 0.0, 3, 0.02,", ", 4, 0.04, ENDREC" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);

    auto status = SG::Import::BDF::BULKDATA::ParseDTI (DB_SERVICEFACTORY, dividedEntryInfo);
    ASSERT_EQ (status, SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::SPECSELData data;
    ASSERT_EQ (DB_SERVICEFACTORY->get<ISPECSELService> ()->FindById (1, data), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (data.m_id, 1);
    ASSERT_STREQ (data.m_TYPE, "A");
    ASSERT_EQ (data.m_TABLESList.size (), 3);

    ASSERT_EQ (data.m_TABLESList[0].m_TID, 2);
    ASSERT_NEAR (data.m_TABLESList[0].m_DAMP, 0.0, TOLERANCE);

    ASSERT_EQ (data.m_TABLESList[1].m_TID, 3);
    ASSERT_NEAR (data.m_TABLESList[1].m_DAMP, 0.02, TOLERANCE);

    ASSERT_EQ (data.m_TABLESList[2].m_TID, 4);
    ASSERT_NEAR (data.m_TABLESList[2].m_DAMP, 0.04, TOLERANCE);
}


TEST_F (DTIParserTest, ParseEntry_SPSEL)
{
    // 模拟多行拼接和部分字段为空的情况
    // 形式: DTI, SPSEL, 1,  , A, 2, 0.0, 3, 0.02,
    //       , 4, 0.04, ENDREC
    std::vector<std::string> Entry = { "DTI, SPSEL, 1,  2, 1, 11, 12" };
    BDF::divideBulkEntry (Entry, dividedEntryInfo);

    auto status = SG::Import::BDF::BULKDATA::ParseDTI (DB_SERVICEFACTORY, dividedEntryInfo);
    ASSERT_EQ (status, SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::SPSELData data;
    ASSERT_EQ (DB_SERVICEFACTORY->get<ISPSELService> ()->FindById (1, data), SG::DataStructure::Common::Status::STATUS_Success);

    ASSERT_EQ (data.m_id, 1);
    ASSERT_EQ (data.m_DAMPL, 2);
    ASSERT_EQ (data.m_FREQL, 1);
    ASSERT_EQ (data.m_Gi.size(), 2);
    ASSERT_EQ (data.m_Gi[0], 11);
    ASSERT_EQ (data.m_Gi[1], 12);
}
